package Vocabulary.View;

import Vocabulary.Word;
import javazoom.jl.player.Player;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import static Vocabulary.MybatisUtils.convertByteArrayToList;
import static Vocabulary.Test.MP3Player.convertByteToInputStream;
import static Youdao.Test.Test.textToSpeech;

public class WordsDetailed extends JFrame {
    ImageIcon background1 = new ImageIcon("src\\image\\p.jpg");
    public WordsDetailed(Word word){
        JPanel Detail=new JPanel(){
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(background1.getImage(),0,0,getWidth(),getHeight(),this);
            }
        };
        Detail.setLayout(new GridLayout(3,1));

        JButton w = new JButton(word.getWords());
        w.setFont(new Font("彩云",Font.BOLD,60));
        w.setBorderPainted(false);
        w.setContentAreaFilled(false);
        Detail.add(w);

        JButton p = new JButton(word.getExplain());
        p.setBorderPainted(false);
        p.setFont(new Font("彩云",Font.BOLD,60));
        p.setContentAreaFilled(false);
        p.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                textToSpeech(p.getText());
            }
            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
        Detail.add(p);




        w.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

                System.out.println("傻逼");
                try {
                    System.out.println(word.getAudio().toString());
                    Player player = new Player(convertByteToInputStream(word.getAudio()));
                    player.play();
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
            @Override
            public void mousePressed(MouseEvent e) {
            }
            @Override
            public void mouseReleased(MouseEvent e) {

            }
            @Override
            public void mouseEntered(MouseEvent e) {

            }
            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
        JButton sentence=new JButton("对不起暂时缺少资源");
        Random random = new Random();
        try {
            List<byte[]> bytes = convertByteArrayToList(word.getInformation(), word.getInformation().length);
            List<String>convertword=new ArrayList<>();
            for (byte[] bytes1:bytes){
                String s = new String(bytes1, StandardCharsets.UTF_8);
                String[] split = s.split("\\.");
                List<String>list= Arrays.stream(split).toList();
                for (String s1:list){
                    String[] split1 = s1.split("\\?");
                    List<String> list1 = Arrays.stream(split1).toList();
                    for (String s2:list1){
                        convertword.add(s2);
                        System.out.println(s2);
                    }
                }
                System.out.println(convertword.size());
                sentence=new JButton(convertword.get(8));
            }
        } catch (Exception e) {
            System.out.println("9999");
        }
        sentence.setBorderPainted(false);
        sentence.setFont(new Font("彩云",Font.BOLD,20));
        sentence.setContentAreaFilled(false);
        Detail.add(sentence);
        JButton finalSentence = sentence;
        sentence.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
//                System.out.println(word.getInformation());
                textToSpeech(finalSentence.getText());
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });

        setContentPane(Detail);
        setBounds(100,100,800,600);
        setVisible(true);
    }
    public static void main(String[] args) {

//        new WordsDetailed(new Word("hello","你好"));
    }
}
